package com.testdemo.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.labmanager.JsonModule.ResponseBase;
import com.testdemo.web.service.GroupService;
import com.testdemo.web.service.LabService;
import com.testdemo.web.service.MyUserService;
import com.testdemo.web.service.ResourceService;
import com.testdemo.web.vo.LabResource;
import com.testdemo.web.vo.Session;
import com.testdemo.web.vo.SessionManager;
import com.testdemo.web.vo.UserInfo;

@Controller
@RequestMapping("/resource")
public class ResourceController {
	
	private SessionManager sessionManager;
	
	public ResourceController() {
		sessionManager = AccountController.sessionManager;
	}
	
	@Resource
	private ResourceService resourceService;
	@Resource
	private MyUserService myUserService;
	@Resource 
	private LabService labService;
	@Resource
	private GroupService groupService;
	
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase AddResource(@RequestBody LabResource labResource) {
		ResponseBase responseBase = new ResponseBase();
		//检查数据
		if(labResource.checkData() == false) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(labResource.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		} 
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "权限不足");
			return responseBase;
		}		
		
		labResource.setLabid(userInfo.getLabid());
		if(resourceService.addResource(labResource) == 0) {
			responseBase.setError(-1, "添加出错");
			return responseBase;
		}
		
		return responseBase;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase DeleteResource(@RequestBody LabResource labResource) {
		ResponseBase responseBase = new ResponseBase();
		//检查数据
		if(labResource.checkData() == false) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(labResource.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "权限不足");
			return responseBase;
		}
		if(resourceService.deleteResource(labResource) == 0) {
			responseBase.setError(-1, "删除失败");
			return responseBase;
		}
		
		
		return responseBase;
	}
	
	@RequestMapping(value = "/apply", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase ApplyResource(@RequestBody LabResource labResource) { 
		ResponseBase responseBase = new ResponseBase();
		//检查数据
		if(labResource.checkData() == false) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(labResource.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		labResource.setUsername(session.getUsername());
		labResource.setIscheck(0);
		if(resourceService.applyResource(labResource) == 0) {
			responseBase.setError(-1, "申请失败");
			return responseBase;
		}
		return responseBase;
	}
	
	@RequestMapping(value = "/getback", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase GetBackResource(@RequestBody LabResource labResource) {
		ResponseBase responseBase = new ResponseBase();
		//检查数据
		if(labResource.checkData() == false) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(labResource.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "权限不足");
			return responseBase;
		}
		if(resourceService.getBackResource(labResource) == 0) {
			responseBase.setError(-1, "归还失败");
			return responseBase;
		}
		
		
		return responseBase;
	}

	
	@RequestMapping(value = "/check", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase checkApplyResource(@RequestBody LabResource labResource) {
		ResponseBase responseBase = new ResponseBase();
		//检查数据
		if(labResource.checkData() == false) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(labResource.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "权限不足");
			return responseBase;
		}
		if(resourceService.checkApplyResource(labResource) == 0) {
			responseBase.setError(-1, "审核申请失败");
			return responseBase;
		}
		
		
		return responseBase;
	}
	
	
	@RequestMapping(value = "/unusedresourcelist", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase getUnuserResource(@RequestBody LabResource labResource) {
		ResponseBase responseBase = new ResponseBase();
		//检查数据
		if(labResource.checkData() == false) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(labResource.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}	
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		labResource.setLabid(userInfo.getLabid());
		List<LabResource> list = resourceService.getUnusedResource(labResource);
		
		responseBase.setData(list);
		return responseBase;
		
	}
	
	@RequestMapping(value = "/applylist", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase getApplyResourceList(@RequestBody LabResource labResource) {
		ResponseBase responseBase = new ResponseBase();
		//检查数据
		if(labResource.checkData() == false) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(labResource.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "权限不足");
			return responseBase;
		}
		labResource.setLabid(userInfo.getLabid());
		List<LabResource> list = resourceService.getApplyList(labResource);
		responseBase.setData(list);
		return responseBase;
		
	}	
	
	@RequestMapping(value = "/borrowlist", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase getBorrowList(@RequestBody LabResource labResource) {
		ResponseBase responseBase = new ResponseBase();
		//检查数据
		if(labResource.checkData() == false) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(labResource.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "权限不足");
			return responseBase;
		}
		labResource.setLabid(userInfo.getLabid());
		List<LabResource> list = resourceService.getBorrowList(labResource);
		responseBase.setData(list);
		return responseBase;
		
	}	
	
	@RequestMapping(value = "/getlist", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase getBorrowListByUsername(@RequestBody LabResource labResource) {
		ResponseBase responseBase = new ResponseBase();
		//检查数据
		if(labResource.checkData() == false) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(labResource.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		List<LabResource> list = resourceService.getBorrowListByUsername(labResource.getUsername());
		responseBase.setData(list);
		return responseBase;
		
	}	
}
